/*
*James Bienz(JB)
*SQL Function Library for Profile
*All CRUD functions for the Profile table are done through this library.
*
*GNU GPL v2
*/

package com.ist412.pocke.SQL;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.ist412.pocke.DTO.ProfileDTO;

public class Profile {
	
	//TableName
	private static final String TABLE_NAME = "Profile";
	
	//Indices of the Profile table
	private static final int ID_INDEX = 0;
	private static final int FNAME_INDEX = 1;
	private static final int LNAME_INDEX = 2;
	private static final int AGE_INDEX = 3;
	private static final int GENDER_INDEX = 4;
	private static final int WEIGHT_INDEX = 5;
	private static final int HFEET_INDEX = 6;
	private static final int HINCH_INDEX = 7;
	private static final int BODY_ARM_INDEX = 8;
	private static final int BODY_CHEST_INDEX = 9;
	private static final int BODY_THIGH_INDEX = 10;
	private static final int BODY_SHOULDER_INDEX = 11;
	private static final int BODY_WAIST_INDEX = 12;
	private static final int BODY_CALF_INDEX = 13;
	
	//Table Keys for Create, Update
	private static final String FNAME_KEY = "fName";
	private static final String LNAME_KEY = "lName";
	private static final String AGE_KEY = "age";
	private static final String GENDER_KEY = "gender";
	private static final String WEIGHT_KEY = "weight";
	private static final String HFEET_KEY = "hFeet";
	private static final String HINCH_KEY = "hInch";
	private static final String BODY_ARM_KEY = "bodyArm";
	private static final String BODY_CHEST_KEY = "bodyChest";
	private static final String BODY_THIGH_KEY = "bodyThigh";
	private static final String BODY_SHOULDER_KEY = "bodyShoulder";
	private static final String BODY_WAIST_KEY = "bodyWaist";
	private static final String BODY_CALF_KEY = "bodyCalf";
	
	//Methods
	
	public static ArrayList<ProfileDTO> getProfile(Context newContext)
	{
		SQLMachine sql = new SQLMachine(newContext);
		SQLiteDatabase db = sql.getReadableDatabase();
		ArrayList<ProfileDTO> profiles = new ArrayList<ProfileDTO>();
		String query = "SELECT * FROM Profile";
		Cursor cursor = db.rawQuery(query, new String[]{});
		cursor.moveToFirst();
		while(cursor.moveToNext()){
			ProfileDTO profile = new ProfileDTO();
			profile.setID(cursor.getInt(ID_INDEX));
			profile.setFName(cursor.getString(FNAME_INDEX));
			profile.setLName(cursor.getString(LNAME_INDEX));
			profile.setAge(cursor.getInt(AGE_INDEX));
			profile.setGender(cursor.getString(GENDER_INDEX));
			profile.setWeight(cursor.getInt(WEIGHT_INDEX));
			profile.setHFeet(cursor.getInt(HFEET_INDEX));
			profile.setHInch(cursor.getInt(HINCH_INDEX));
			profile.setBodyArm(cursor.getInt(BODY_ARM_INDEX));
			profile.setBodyChest(cursor.getInt(BODY_CHEST_INDEX));
			profile.setBodyThigh(cursor.getInt(BODY_THIGH_INDEX));
			profile.setBodyShoulder(cursor.getInt(BODY_SHOULDER_INDEX));
			profile.setBodyWaist(cursor.getInt(BODY_WAIST_INDEX));
			profile.setBodyCalf(cursor.getInt(BODY_CALF_INDEX));
			profiles.add(profile);
		}
		return profiles;
	}
	
	/* Creates a new Profile in the Database
	 * 	
	 * @params Context is the context of the android application
	 * @params ProfileDTO the DTO for all the data retrieved from the database
	 * 
	 * @return long the row id of the newly inserted row. (-1 if there was an error)
	 */
	public static long createProfile(Context newContext, ProfileDTO newProfile){
		SQLMachine sql = new SQLMachine(newContext);
		SQLiteDatabase db = sql.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(FNAME_KEY, newProfile.getFName());
		values.put(LNAME_KEY, newProfile.getLName());
		values.put(AGE_KEY, newProfile.getAge());
		values.put(GENDER_KEY, newProfile.getGender());
		values.put(WEIGHT_KEY, newProfile.getWeight());
		values.put(HFEET_KEY, newProfile.getHFeet());
		values.put(HINCH_KEY, newProfile.getHInch());
		values.put(BODY_ARM_KEY, newProfile.getBodyArm());
		values.put(BODY_CHEST_KEY, newProfile.getBodyChest());
		values.put(BODY_THIGH_KEY, newProfile.getBodyThigh());
		values.put(BODY_SHOULDER_KEY, newProfile.getBodyShoulder());
		values.put(BODY_WAIST_KEY, newProfile.getBodyWaist());
		values.put(BODY_CALF_KEY, newProfile.getBodyCalf());
		return db.insert(TABLE_NAME, null, values);
	}
	
	/*
	 * Updates Profile in Sqlite database.
	 * 
	 * Utilizes the SQLMachine to open the SQLite database and then updates the
	 * profile based on the new information.
	 * 
	 * @param newContext the context from the android application
	 * @param profile the ProfileDTO that contains the updated profile information
	 * 
	 * @return int of the number of rows effected
	 */
	public static int updateProfile(Context newContext, ProfileDTO profile){
		SQLMachine sql = new SQLMachine(newContext);
		SQLiteDatabase db = sql.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(FNAME_KEY, profile.getFName());
		values.put(LNAME_KEY, profile.getLName());
		values.put(AGE_KEY, profile.getAge());
		values.put(GENDER_KEY, profile.getGender());
		values.put(WEIGHT_KEY, profile.getWeight());
		values.put(HFEET_KEY, profile.getHFeet());
		values.put(HINCH_KEY, profile.getHInch());
		values.put(BODY_ARM_KEY, profile.getBodyArm());
		values.put(BODY_CHEST_KEY, profile.getBodyChest());
		values.put(BODY_THIGH_KEY, profile.getBodyThigh());
		values.put(BODY_SHOULDER_KEY, profile.getBodyShoulder());
		values.put(BODY_WAIST_KEY, profile.getBodyWaist());
		values.put(BODY_CALF_KEY, profile.getBodyCalf());
		String filter = "id=" + profile.getID();
		return db.update(TABLE_NAME, values, filter, null);
	}
}
